package io.renren.modules.noc.service.impl;

import io.renren.modules.noc.dao.NocRoleDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.noc.dao.NocTemplateDao;
import io.renren.modules.noc.entity.NocTemplateEntity;
import io.renren.modules.noc.service.NocTemplateService;


@Service("nocTemplateService")
public class NocTemplateServiceImpl extends ServiceImpl<NocTemplateDao, NocTemplateEntity> implements NocTemplateService {

    @Autowired
    private NocRoleDao roleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NocTemplateEntity> page = this.page(
                new Query<NocTemplateEntity>().getPage(params),
                new QueryWrapper<NocTemplateEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<NocTemplateEntity> queryByRole(){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        List<NocTemplateEntity> list = new ArrayList<>();
        if("admin".equals(userEntity.getUsername())){
            list = this.list();
        }else{
            Map<String,Object> roleMap = new HashMap<>();
            roleMap.put("uid",userEntity.getUserId());
            List<String> in = roleDao.getUserTidRole(roleMap);
            if(in.size()>0) {
                list = (List<NocTemplateEntity>) this.listByIds(in);
            }
        }
        return list;
    }

}
